/**
 * Created by Ethan.Yang on 2016/8/23.
 * Changed by Guang Li on 2016/8/23.
 */
define(['jquery', 'model/global', 'appMessageBox', 'appConst'], function ($, global, msgBox, appConst) {
    return {
        searchDc: function (params, callback, errCallBack) {
            $.ajax({
                url: 'analysis/tolerance/dc',
                type: 'POST',
                dataType: 'JSON',
                "data": params,
                success: function (data) {
                    if (data.code == 0) {
                        msgBox.errorBox(data.message);
                        if (typeof errCallBack == 'function') {
                            errCallBack();
                        }
                    } else {
                        if (typeof callback == 'function') {
                            callback(data.body);
                        }
                    }
                },
                error: function (e) {
                    if (e.status == 401) {
                        msgBox.sessionFailureBox();
                    }
                    if (e.status == 500) {
                        msgBox.serverFailureBox();
                    }
                    if (e.status == 404) {
                        msgBox.pathFailureBox();
                    }
                    if (typeof errCallBack == 'function') {
                        errCallBack();
                    }
                }
            });
        },

        updateItem: function (param, callback, errCallBack) {
            $.ajax({
                url: 'analysis/tolerance/updateItem',
                type: 'POST',
                dataType: 'JSON',
                contentType: "application/json",
                data: JSON.stringify(param),
                success: function (data) {
                    if (data.code == 0) {
                        msgBox.errorBox(data.message);
                        if (typeof errCallBack == 'function') {
                            errCallBack();
                        }
                    } else {
                        msgBox.successBox({
                            title: $.i18n.prop("MESSAGEBOX_TITLE_TIPS"),
                            content: $.i18n.prop("ITEM_UPDATE_SUCCESS")
                        });
                        if (typeof callback == 'function') {
                            callback(data.body);
                        }
                    }
                },
                error: function (e) {
                    if (e.status == 401) {
                        msgBox.sessionFailureBox();
                    }
                    if (e.status == 500) {
                        msgBox.serverFailureBox();
                    }
                    if (e.status == 404) {
                        msgBox.pathFailureBox();
                    }
                    if (typeof errCallBack == 'function') {
                        errCallBack();
                    }
                }
            });
        },

        addItemName: function (param, callback, errCallBack) {
            $.ajax({
                url: 'analysis/tolerance/addItem',
                type: 'POST',
                dataType: 'JSON',
                contentType: "application/json",
                data: JSON.stringify(param),
                success: function (data) {
                    if (data.code == 0) {
                        msgBox.errorBox(data.message);
                        if (typeof errCallBack == 'function') {
                            errCallBack();
                        }
                    } else {
                        msgBox.successBox({
                            title: $.i18n.prop("MESSAGEBOX_TITLE_TIPS"),
                            content: $.i18n.prop("ITEM_SAVE_SUCCESS")
                        });
                        if (typeof callback == 'function') {
                            callback(data.body);
                        }
                    }
                },
                error: function (e) {
                    if (e.status == 401) {
                        msgBox.sessionFailureBox();
                    }
                    if (e.status == 500) {
                        msgBox.serverFailureBox();
                    }
                    if (e.status == 404) {
                        msgBox.pathFailureBox();
                    }
                    if (typeof errCallBack == 'function') {
                        errCallBack();
                    }
                }
            });
        },

        deleteItem: function (param, callback, errCallBack) {
            $.ajax({
                url: 'analysis/tolerance/deleteItem',
                type: 'POST',
                dataType: 'JSON',
                "data": param,
                success: function (data) {
                    if (data.code == 0) {
                        msgBox.errorBox(data.message);
                        if (typeof errCallBack == 'function') {
                            errCallBack();
                        }
                    } else {
                        msgBox.successBox({
                            title: $.i18n.prop("MESSAGEBOX_TITLE_TIPS"),
                            content: $.i18n.prop("ITEM_DELETE_SUCCESS")
                        });
                        if (typeof callback == 'function') {
                            callback(data.body);
                        }
                    }

                },
                error: function (e) {
                    if (e.status == 401) {
                        msgBox.sessionFailureBox();
                    }
                    if (e.status == 500) {
                        msgBox.serverFailureBox();
                    }
                    if (e.status == 404) {
                        msgBox.pathFailureBox();
                    }
                    if (typeof errCallBack == 'function') {
                        errCallBack();
                    }
                }
            });
        },

        createDimensionChain: function (param, callback, errCallBack) {
            $.ajax({
                url: 'analysis/tolerance/createDC',
                type: 'POST',
                dataType: 'JSON',
                contentType: "application/json",
                data: JSON.stringify(param),
                success: function (data) {
                    if (data.code == 0) {
                        if (data.message == "EXCEPTION_GLOBAL_OBJECT_ALREADY_EXISTS") {
                            msgBox.errorBox("EXCEPTION_GLOBAL_OBJECT_ALREADY_EXISTS", "SCHEME_VALUE");
                        } else {
                            msgBox.errorBox(data.message);
                        }

                        if (typeof errCallBack == 'function') {
                            errCallBack();
                        }
                    } else {
                        msgBox.successBox({
                            title: $.i18n.prop("MESSAGEBOX_TITLE_TIPS"),
                            content: $.i18n.prop("SCHEME_CREATE_SUCCESS")
                        });
                        if (typeof callback == 'function') {
                            callback(data.body);
                        }
                    }
                },
                error: function (e) {
                    if (e.status == 401) {
                        msgBox.sessionFailureBox();
                    }
                    if (e.status == 500) {
                        msgBox.serverFailureBox();
                    }
                    if (e.status == 404) {
                        msgBox.pathFailureBox();
                    }
                    if (typeof errCallBack == 'function') {
                        errCallBack();
                    }
                }
            });
        },

        analyseNDC: function (param, callback, errCallBack) {
            $.ajax({
                url: 'analysis/tolerance/toleranceAnalysis',
                type: 'POST',
                dataType: 'JSON',
                "data": param,
                success: function (data) {
                    if (data.code == 0) {
                        msgBox.errorBox(data.message);
                        if (typeof errCallBack == 'function') {
                            errCallBack();
                        }
                    } else {
                        if (typeof callback == 'function') {
                            callback(data.body);
                        }
                    }
                },
                error: function (e) {
                    if (e.status == 401) {
                        msgBox.sessionFailureBox();
                    }
                    if (e.status == 500) {
                        msgBox.serverFailureBox();
                    }
                    if (e.status == 404) {
                        msgBox.pathFailureBox();
                    }
                    if (typeof errCallBack == 'function') {
                        errCallBack();
                    }
                }
            });
        },

        exportResult: function (param, callback, errCallBack) {
            $.ajax({
                url: 'analysis/tolerance/result',
                type: "POST",
                dataType: 'json',
                data: param,
                success: function (data) {
                    if (data.code == 0) {
                        msgBox.errorBox(data.message);
                        if (typeof errCallBack == 'function') {
                            errCallBack();
                        }
                    } else {
                        if (typeof callback == 'function') {
                            callback(data.body);
                        }
                    }
                }
            })
        },

        isExist: function (param, callback, errCallBack) {
            $.ajax({
                url: 'analysis/tolerance/checkToleranceName',
                type: "POST",
                dataType: 'json',
                data: param,
                success: function (data) {
                    if (data.code == 0) {
                        msgBox.errorBox(data.message);
                        if (typeof errCallBack == 'function') {
                            errCallBack();
                        }
                    } else {
                        if (typeof callback == 'function') {
                            callback(data.body);
                        }
                    }
                }
            })
        }
    };
})